package itheima.controller;

import itheima.pojo.Article;
import itheima.pojo.Category;
import itheima.pojo.PageBean;
import itheima.pojo.Result;
import itheima.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    /**
     * 新增文章
     *
     * @param article
     * @return
     */
    @PostMapping
    public Result add(@RequestBody @Validated(Category.Add.class) Article article) {
        articleService.add(article);
        return Result.success();
    }

    /**
     * 文章列表
     * @param page
     * @param pageSize
     * @param categoryId
     * @param state
     * @return
     */
    @GetMapping
    public Result<PageBean<Article>> list(@RequestParam(defaultValue = "1") Integer page,
                                          @RequestParam(defaultValue = "10") Integer pageSize,
                                          @RequestParam(required = false) Integer categoryId,
                                          @RequestParam(required = false) String state) {
        PageBean<Article> pageBean =articleService.list(page, pageSize, categoryId, state);
        return Result.success(pageBean);
    }


    /**
     * 文章详情
     * @param id
     * @return
     */
    @GetMapping("/detail")
    public Result<Article> detail(Integer id) {
        Article article = articleService.detail(id);
        return Result.success(article);
    }


    /**
     * 更新文章
     * @param article
     * @return
     */
    @PutMapping
    public Result update(@RequestBody @Validated(Category.Update.class) Article article) {
        articleService.update(article);
        return Result.success();
    }

    /**
     * 根据id删除文章
     * @param id
     * @return
     */
    @DeleteMapping
    public Result delete(Integer id) {
        articleService.delete(id);
        return Result.success();
    }


}
